package com.zhentao.login.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.login.mapper.TMenuMapper;
import com.zhentao.login.mapper.TRoleMapper;
import com.zhentao.login.mapper.TUserMapper;
import com.zhentao.login.service.TUserService;
import com.zhentao.nlz.project.login.TRole;
import com.zhentao.nlz.project.login.TRole;
import com.zhentao.login.utils.JwtToken;
import com.zhentao.nlz.project.login.TUser;
import com.zhentao.nlz.project.product.TDept;
import com.zhentao.nlz.project.product.vo.UserVo;

import com.zhentao.nlz.project.product.TDept;
//import com.zhentao.nlz.project.product.vo.UserVo;

import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Autowired;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 不.凌
 * @description 针对表【t_user(用户表)】的数据库操作Service实现
 * @createDate 2024-09-22 20:51:42
 */
@Service
public class TUserServiceImpl extends ServiceImpl<TUserMapper, TUser>
        implements TUserService, UserDetailsService {
    @Resource
    private TMenuMapper menuMapper;



    @Autowired
    private TUserMapper userMapper;
    @Autowired
    private TRoleMapper roleMapper;


    @Autowired
    private JwtToken jwtToken;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        TUser user= this.getOne(Wrappers.<TUser>lambdaQuery().eq(TUser::getUsername,username));
        List<String> strings=menuMapper.selectPermsByUser(user);
        List<GrantedAuthority> list=new ArrayList<>();
        for (String string : strings) {
            SimpleGrantedAuthority sga=new SimpleGrantedAuthority(string);
            list.add(sga);
        }
        user.setPerms(list);
        return user;
    }
//查询用户
@Override
public List<UserVo> getUser(UserVo userVo) {
    // 创建分页对象
    IPage<UserVo> page = new Page<>(userVo.getPageNum(), userVo.getPageSize());

    // 创建LambdaQueryWrapper来构造查询条件
    LambdaQueryWrapper<UserVo> wrapper = new LambdaQueryWrapper<UserVo>();
    LambdaQueryWrapper<UserVo> like = Wrappers.<UserVo>lambdaQuery()
            .like(StringUtils.isNotBlank(userVo.getUsername()), UserVo::getUsername, userVo.getUsername());
    // 如果username不为空，则添加like条件
    wrapper.like(StringUtils.isNotBlank(userVo.getUsername()), UserVo::getUsername, userVo.getUsername());

    // 执行查询并获取结果列表
    List<UserVo> userList = userMapper.selectUser();
    System.out.println(userList+"11111111111111");
    // 返回用户列表
    return userList;
}
    //查询用户
//    @Override
//    public List<UserVo> getUser(UserVo userVo) {
//        // 创建分页对象
//        IPage<UserVo> page = new Page<>(userVo.getPageNum(), userVo.getPageSize());
//        // 创建LambdaQueryWrapper来构造查询条件
//        LambdaQueryWrapper<UserVo> wrapper = new LambdaQueryWrapper<UserVo>();
//        LambdaQueryWrapper<UserVo> like = Wrappers.<UserVo>lambdaQuery()
//                .like(StringUtils.isNotBlank(userVo.getUsername()), UserVo::getUsername, userVo.getUsername());
//        // 如果username不为空，则添加like条件
//        wrapper.like(StringUtils.isNotBlank(userVo.getUsername()), UserVo::getUsername, userVo.getUsername());
//        // 执行查询并获取结果列表
//        List<UserVo> userList = userMapper.selectUser();
//        // 返回用户列表
//        return userList;
//    }
    //查询用户


    @Override
    public TUser getUsers() {
        System.out.println("执行getUserinfo方法");
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        String token = request.getHeader("token");
        String substring = token.substring(1, token.length() - 1);
        //根据token获取用户id
        TUser user = (TUser) jwtToken.getLoginUser(substring);
        System.out.println(user);
        System.out.println(token+"token111");
        return user;
}

//    @Override
//    @Transactional
//    public boolean updateUserInfo(TUser user) {
//        user.setUpdateTime(new Date());
//        System.out.println(user);
//        this.updateById(user);
//        return true;
//    }

    @Override
    @Transactional
    public boolean updateUserInfo(TUser user) {
        user.setUpdateTime(new Date());
        System.out.println(user);
        this.updateById(user);
        return true;
    }

    @Override
    @Transactional
    public boolean saveUser(TUser user) {
        user.setCreateTime(new Date());
        int insert = userMapper.insert(user);
        return true;
    }

    @Override
    public TUser getUserById(Integer userId) {
        TUser tUser = userMapper.selectById(userId);
        return tUser;
    }
}


